import {
  editProFileAPI,
  getUserInfoAPI,
  getUserProfileAPI,
  updatePhotoAPI,
} from '../../api/user';
import { AppDispatch } from '../reducer';
import { ProFileType } from '../reducer/user';

/**获取用户信息Action */
export function getUserInfoAction() {
  return async (dispatch: AppDispatch) => {
    let res = await getUserInfoAPI();
    // console.log('----', res);
    // 存数据
    dispatch({ type: 'user/saveUseInfo', payload: res.data });
  };
}

/** 获取个人资料Action */
export function getUserProfileAction() {
  return async (dispacth: AppDispatch) => {
    const res = await getUserProfileAPI();
    // console.log(res);
    // 存数据
    dispacth({ type: 'user/saveProfile', payload: res.data });
  };
}

/** 更新头像Action */
export function updatePhotoAction(data: FormData) {
  return async (dispacth: AppDispatch) => {
    const res = await updatePhotoAPI(data);
    console.log(res);
    // return res;
    // 修改数据
    dispacth({ type: 'user/updateProfile', payload: res.data });
  };
}

/** 编辑个人资料Action */
export function editProFileAction(data: Partial<ProFileType>) {
  return async (dispatch: AppDispatch) => {
    await editProFileAPI(data);
    dispatch({ type: 'user/updateProfile', payload: data });
  };
}
